package com.fishingwithme.android.ui.screens.my.maintain

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Approval
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.navigation.NavController
import com.fishingwithme.android.R
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.ui.screens.my.MyScreenHiltEntryPoint
import com.fishingwithme.android.ui.theme.AppDimensions
import dagger.hilt.android.EntryPointAccessors

data class FisherCenterItem(
    val role: String,
    val title: String,
    val description: String,
    val icon: @Composable () -> Unit,
    val route: String
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FisherCenter(navController: NavController) {
    val context = LocalContext.current
    val preferencesManager = EntryPointAccessors.fromApplication(
        context.applicationContext,
        MyScreenHiltEntryPoint::class.java
    ).preferencesManager()

    // Get user data
    val user by preferencesManager.getLoginUserObj().collectAsState(initial = null)
    val fisherCenterItems = listOf(
        FisherCenterItem(
            role = "ROLE_ADMIN",
            title = stringResource(R.string.activity_settings),
            description = stringResource(R.string.activity_settings_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.ACTIVITIES_SETTINGS
        ),
        FisherCenterItem(
            role = "ROLE_USER",
            title = stringResource(R.string.my_to_be_circle_maintainer),
            description = stringResource(R.string.to_be_circle_maintain_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.TO_BE_CIRCLE_MAINTAINER
        ),

        FisherCenterItem(
            role = "ROLE_USER",
            title = stringResource(R.string.points_settings),
            description = stringResource(R.string.points_settings_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.POINTS_SETTINGS
        ),
        FisherCenterItem(
            role = "ROLE_USER",
            title = stringResource(R.string.credit_lvl_rules),
            description = stringResource(R.string.credit_lvl_rules_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.CREDIT_LVL
        ),
        FisherCenterItem(
            role = "ROLE_USER",
            title = stringResource(R.string.credit_rules),
            description = stringResource(R.string.credit_how_to_get_points),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.CREDIT_RULES
        ),
        FisherCenterItem(
            role = "ROLE_CIRCLE_MAINTAINER",
            title = stringResource(R.string.my_spots_maintain),
            description = stringResource(R.string.spot_maintain_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Place,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.SPOT_APPROVE
        ),
        FisherCenterItem(
            role = "ROLE_CIRCLE_MAINTAINER",
            title = stringResource(R.string.my_harvest_maintain),
            description = stringResource(R.string.harvest_maintain_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.HARVEST_APPROVE
        ),

        FisherCenterItem(
            role = "ROLE_ADMIN",
            title = stringResource(R.string.circle_approval),
            description = stringResource(R.string.circle_approval_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.CIRCLE_SETTINGS

        ),
        FisherCenterItem(
            role = "ROLE_ADMIN",
            title = stringResource(R.string.circle),
            description = stringResource(R.string.circle_desc),
            icon = {
                Icon(
                    imageVector = Icons.Default.Approval,
                    contentDescription = null,
                    tint = MaterialTheme.colorScheme.primary
                )
            },
            route = FishingNavigation.AUDITOR_APPROVE
        )

    ).filter { it ->
        user?.roles?.contains(it.role) ?: false
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.fisher_center)) },
                navigationIcon = {
                    IconButton(onClick = { navController.popBackStack() }) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = stringResource(R.string.back)
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.surface
                )
            )
        }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(AppDimensions.SPACING_16)
        ) {
            LazyVerticalGrid(
                columns = GridCells.Fixed(2),
                horizontalArrangement = Arrangement.spacedBy(AppDimensions.SPACING_16),
                verticalArrangement = Arrangement.spacedBy(AppDimensions.SPACING_16),
                modifier = Modifier.fillMaxSize()
            ) {
                items(fisherCenterItems) { item ->
                    MaintenanceCard(
                        item = item,
                        onClick = {
                            navController.navigate(item.route)
                        }
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MaintenanceCard(
    item: FisherCenterItem,
    onClick: () -> Unit
) {
    Card(
        modifier = Modifier
            .fillMaxWidth(),
        elevation = CardDefaults.cardElevation(defaultElevation = AppDimensions.CARD_ELEVATION_2),
        onClick = onClick
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppDimensions.SPACING_16),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            item.icon()

            Text(
                text = item.title,
                style = MaterialTheme.typography.titleMedium,
                modifier = Modifier.padding(top = AppDimensions.SPACING_8),
                textAlign = TextAlign.Center
            )

            Text(
                text = item.description,
                style = MaterialTheme.typography.bodySmall,
                modifier = Modifier.padding(top = AppDimensions.SPACING_4),
                textAlign = TextAlign.Center,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
    }
}